<template>
  <div>
    <en-table-layout toolbar pagination :tableData="tableData" :loading="loading" @closeDrawer="showDrawer = false">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入商品名称" />
        </div>
      </div>
      <template slot="table-columns">
        <el-table-column label="商品名称" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="商品编号">
          <template slot-scope="scope">{{ scope.row.sn }}</template>
        </el-table-column>
        <el-table-column label="部门名称">
          <template slot-scope="scope">{{ scope.row.dept_name }}</template>
        </el-table-column>
        <el-table-column label="仓库名称">
          <template slot-scope="scope">{{ scope.row.warehouse_name }}</template>
        </el-table-column>
        <el-table-column label="规格">
          <template slot-scope="scope">{{ scope.row.specification }}</template>
        </el-table-column>
        <el-table-column label="库存数量">
          <template slot-scope="scope">{{ scope.row.stock_num }}</template>
        </el-table-column>
        <el-table-column label="产品库存预警数">
          <template slot-scope="scope">{{ scope.row.warning_value }}</template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.create_time | unixToDate }}</template>
        </el-table-column>
      </template>

      <el-pagination slot="pagination" v-if="pageData" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="pageData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="pageData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="pageData.data_total">
      </el-pagination>

    </en-table-layout>
  </div>
</template>

<script>
import * as API_goods from '@/api/goods'

export default {
  name: 'understock',
  data() {
    return {
      /** 列表loading状态 */
      loading: false,

      /** 列表参数 */
      params: {
        page_no: 1,
        page_size: 10
      },

      /** 列表数据 */
      tableData: [],

      /** 列表分页数据 */
      pageData: []
    }
  },
  mounted() {
    this.GET_WarningGoodsList()
  },
  watch: {
    $route: {
      immediate: true,
      handler(newVal) {
        if (newVal.query.name) {
          this.params.name = newVal.query.name
          this.advancedForm.name = newVal.query.name
        }
      }
    }
  },
  methods: {
    handlePreview(row) {
      this.showDrawer = true
      this.currentRow = row
    },

    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_WarningGoodsList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_WarningGoodsList()
    },

    /** 切换商品类型 */
    changeGoodsType(val) {
      delete this.params.goods_type
      if (val !== '') {
        this.params = {
          ...this.params,
          goods_type: val
        }
      }
      this.GET_WarningGoodsList()
    },

    /** 切换分组*/
    changeGoodsCateGory(data) {
      delete this.params.shop_cat_path
      if (data && Array.isArray(data) && data.length !== 0) {
        this.params = {
          ...this.params,
          shop_cat_path: '0|' + data.join('|') + '|'
        }
      }
      this.GET_WarningGoodsList()
    },
    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        page_no: 1,
        name: data
      }
      this.GET_WarningGoodsList()
    },

    GET_WarningGoodsList() {
      this.loading = true
      API_goods.getWarningGoodsList(this.params).then(response => {
        this.loading = false
        this.pageData = {
          page_no: response.page_no,
          page_size: response.page_size,
          data_total: response.data_total
        }
        this.tableData = response.data
      })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
/deep/ div.toolbar {
  height: 70px;
  padding: 20px 0;
}

/deep/ .el-table td:not(.is-left) {
  text-align: center;
}

.inner-toolbar {
  display: flex;
  width: 100%;
}

.toolbar-search {
  margin-right: 10px;
}

div.toolbar-btns {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: flex-start;
  align-items: center;

  div {
    span {
      display: inline-block;
      font-size: 14px;
      color: #606266;
    }
  }

  .conditions {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: flex-start;
    align-items: center;
    min-width: 24.5%;

    .choose-machine {
      width: 60%;
    }
  }
}

/deep/ .popinfo {
  .toolbar {
    display: none;
  }

  .el-dialog__body {
    border: 1px solid #e5e5e5;

    .el-table {
      border: 1px solid #e5e5e5;
    }
  }
}

.goods-info {
  display: flex;
  align-items: center;

  .drawer-text {
    color: #006BB4;
    cursor: pointer;

    &:hover {
      text-decoration: underline;
    }
  }

  .goods-image {
    img {
      width: 80px;
      height: 80px;
    }
  }

  .goods-name-box {
    text-align: left;

    .goods-name {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
    }
  }
}
</style>
